<?php

namespace app\admin\controller;

use think\facade\Request;
use app\common\Common;
use app\admin\service\BlackService;

class Black extends Base
{
    private $service;

    public function __construct(BlackService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    public function index(){
        return view();
    }

    public function list(Request $request)
    {
        $params = [
            'uid' => $request::get('uid'),
            'phone' => $request::get('phone'),
            'start_date' => $request::get('start_date'),
            'end_date' => $request::get('end_date'),
            'page' => $request::get('page', 1),
            'limit' => $request::get('limit', 20)
        ];

        try {
            validate(\app\admin\validate\Black::class)->scene('user_black_list')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getUserBlackList($params);

        return Common::success($data);
    }

    public function addUserBlack(Request $request)
    {
        $params = [
            'uid' => $request::post('uid'),
            'reason' => $request::post('reason')
        ];

        try {
            validate(\app\admin\validate\Black::class)->scene('add_user_black')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->addUserBlack($params);

        if (isset($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success();
    }

    public function cancelUserBlack(Request $request)
    {
        $params = [
            'uid' => $request::post('uid')
        ];

        try {
            validate(\app\admin\validate\Black::class)->scene('cancel_user_black')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->cancelUserBlack($params);

        if (isset($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success();
    }
}
